// constants
#define closed 0
#define connecting 1
#define connected 2

#define button_key0 D5
#define button_key1 D6

// variables
int state = 0;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(button_key0, INPUT);
  pinMode(button_key1, INPUT);

  Serial.begin(115200); // opens serial port, sets data rate to 115200 bps
  Serial.println();
}

// the loop function runs over and over again forever
void loop() {
  if (digitalRead(button_key0) == HIGH)
    connect();
  if (digitalRead(button_key1) == HIGH)
    close();

  switch (state) {
    case 2:
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED on by making the voltage LOW
      break;
    case 1:
      blink();
      break;
    case 0:
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED off (HIGH is the voltage level)
      break;
  }
  delay(1);
}

void blink() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

void connect() {
  Serial.println("Connection establishment");
  state = connected;
}

void close() {
  Serial.println("Connection closed");
  state = closed;
}
